package com.xuecheng.messagesdk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xuecheng.messagesdk.mapper.MqMessageHistoryMapper;
import com.xuecheng.messagesdk.model.MqMessageDo;
import com.xuecheng.messagesdk.model.enums.MqMessageStageEnum;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.mapper.MqMessageMapper;
import com.xuecheng.messagesdk.service.MqMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NegativeOrZero;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class MqMessageServiceImpl extends ServiceImpl<MqMessageMapper, MqMessage> implements MqMessageService {

    @Autowired
    private MqMessageMapper mqMessageMapper;

    @Override
    public void saveMessage(MqMessageDo mqMessageDo) {
        MqMessage mqMessage = BeanUtil.copyProperties(mqMessageDo, MqMessage.class);
        mqMessageMapper.insert(mqMessage);
    }

    @Override
    public List<MqMessage> getWaitTaskList(Integer shardIndex, Integer shardTotal, Integer total) {
        return mqMessageMapper.getWaitTaskList(shardIndex, shardTotal, total);
    }

    @Override
    public void updateStageState(Integer num, Long id) {
        if (MqMessageStageEnum.ONE.getCode().equals(num)){
            mqMessageMapper.updateStageOneState(id);
        } else if (MqMessageStageEnum.TWO.getCode().equals(num)){
            mqMessageMapper.updateStageTwoState(id);
        } else {
            mqMessageMapper.updateStageThreeState(id);
        }
    }
}
